package son.nt;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import son.nt.util.GoogleAnaTool;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.Toast;

public class MainActivity extends TabActivity implements TabContentFactory,OnTabChangeListener{
	private static final String CATEGORY_ACT1 = MainActivity.class.getPackage().getName() + ".ACT1";
	private static final String TAG = "MainActivity";
	private TabHost tabHost;
	private static final long EXIT_INTERVAL = 2000L;
	private long exitTimer = Long.MIN_VALUE;
	private boolean isFinish = false;
	private Context mContext = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "oncreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mContext = this;
		GoogleAnaTool.start(mContext);
		GoogleAnaTool.track("/");
		Holder.createInstance(mContext);
		tabHost = getTabHost();
		final PackageManager pm = getPackageManager();
		Intent act1Intent = new Intent(Intent.ACTION_MAIN, null);
		act1Intent.addCategory(CATEGORY_ACT1);
		
		List<ResolveInfo> list = pm.queryIntentActivities(act1Intent, 0);
		for(ResolveInfo info : list){
			TabItem tabItem = new TabItem();
			tabItem.tag = info.activityInfo.name;
			tabItem.label = info.loadLabel(pm);
			tabItem.icon = info.activityInfo.loadIcon(pm);
			ActivityItem activityItem = new ActivityItem();
			activityItem.id = tabItem.tag;
			ComponentName component = new ComponentName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
			activityItem.intent = new Intent()
				.setComponent(component);
//			tabItem.intent = new Intent()
//				.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,
//						info.activityInfo.name));
			tabItem.activityList.add(activityItem);
			Holder.getInstance().tabItemMap.put(tabItem.tag, tabItem);
			setupTab(tabItem);
		}
		
	}
//	@Override
//	public boolean dispatchKeyEvent(KeyEvent event) {
//		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
//			isFinish = true;
//			
//			if(isFinish){
//				if((exitTimer + EXIT_INTERVAL) < System.currentTimeMillis()){
//					Toast.makeText(getBaseContext(), "Back again to exit", Toast.LENGTH_SHORT).show();
//					exitTimer = System.currentTimeMillis();
//				}else{
//					Toast.makeText(getBaseContext(), "Back again to exit2", Toast.LENGTH_SHORT).show();
//					isFinish = true;
////					finish();
//					System.exit(0);
//				}
//			}
//			return true;
//		}
//		return true;
//	}
	
	private void setupTab(TabItem tabItem) {
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec = tabHost.newTabSpec(tabItem.tag);
		spec.setIndicator(tabItem.label, tabItem.icon);
		spec.setContent(tabItem.activityList.get(0).intent);
		tabHost.addTab(spec);
	}

	private static class TabItem{
		private String tag = null;
    	private CharSequence label = null;
    	private Drawable icon = null;
    	List<ActivityItem> activityList = new ArrayList<MainActivity.ActivityItem>();
	}
	private static class ActivityItem{
		private String id = null;
		private Intent intent = null;
		private boolean marked = false;
		private boolean notab = false;
	}
	
	public static void pushActivity(Activity caller, Intent...intents){
		Log.v(TAG, "push activity");
		Activity parent = caller.getParent();
		if(parent instanceof MainActivity){
			MainActivity mainActivity = (MainActivity)parent;
			mainActivity.pushActivity("", null, intents);
		}
	}
	private String findTabTag(Activity activity){
		return null;
	}
	public View pushActivity(String tag, Boolean isAnimation, Intent...intents){
		final LocalActivityManager lam = getLocalActivityManager();
		TabItem tabItem = Holder.getInstance().tabItemMap.get(tag);
		if(isAnimation == null){
			
		}
		View view = null;
		for (int i = 0; i < intents.length; i++) {
			String id = tag + ":" + Holder.id ++;
			Intent intent = intents[i];
			ActivityItem activityItem = new ActivityItem();
			activityItem.id = "";
			activityItem.intent = intent;
			activityItem.notab = isAnimation.booleanValue();
			tabItem.activityList.add(activityItem);
			
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Window window = lam.startActivity(id, intent);
			intent.setFlags(0);
			view = window.getDecorView();
		}
		return view;
	}
	
	public static void pushActivity(Activity caller, Intent intents){
		Log.v(TAG, "push activity");
		Activity parent = caller.getParent();
//		if(parent instanceof MainActivity){
//			Log.v(TAG, "parent instanceof MainActivity");
//			MainActivity mainActivity = (MainActivity)parent;
//			mainActivity.pushActivity("AreaActivity", intents);
//			
//		}else{
//			Log.v(TAG, "NOT parent instanceof MainActivity");
//			MainActivity mainActivity = (MainActivity)parent;
//		}
		MainActivity mainActivity = (MainActivity)parent;
		mainActivity.pushActivity("AreaActivity", intents);
		
	}
	
//	public static void pushActivity(String tag, Intent intent){
//		Activity parent = call
//	}
	
	public View pushActivity(String tag, Intent intents){
		final LocalActivityManager lam = getLocalActivityManager();
////		TabItem tabItem = Holder.getInstance().tabItemMap.get(tag);
//		TabItem tabItem = new TabItem();
		View view = null;
//			String id = tag + ":" + Holder.id ++;
//			Intent intent = intents;
//			ActivityItem activityItem = new ActivityItem();
//			activityItem.id = id;
//			activityItem.intent = intent;
//			tabItem.activityList.add(activityItem);
//			
			intents.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Window window = lam.startActivity(tag, intents);
//			intent.setFlags(0);
			view = window.getDecorView();
			setContentView(view);
			return view;
		 
//		View view = getLocalActivityManager().startActivity(
//				"ReferenceName", intents);
//				new Intent(this, YourActivity.class)
//						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
//				.getDecorView();
//		this.setContentView(view);
	}
	public static class Holder{
		public static int id = 0;
		private static  Holder instance = null;
		Context mContext;
		private final Map<String, TabItem> tabItemMap = new LinkedHashMap<String, TabItem>();
		public Holder(Context mContext){
			this.mContext = mContext;
		}
		static Holder createInstance(Context mContext){
			instance = new Holder(mContext);
			return instance;
		}
		public static Holder getInstance(){
			return instance;
		}
		
	}

	@Override
	public void onContentChanged() {
		Log.v(TAG, "onContentChanged");
		super.onContentChanged();
	}
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.v(TAG, "onConfigurationChanged");
		super.onConfigurationChanged(newConfig);
	}
	public void onTabChanged(String tabId) {
		Log.v(TAG, "onTabChanged");
	}
	public View createTabContent(String tag) {
		Log.v(TAG, "createTabContent");
		return null;
	}
	
//	@Override
//	protected void onNewIntent(Intent intent) {
//		// TODO Auto-generated method stub
//		Log.v(TAG, "on new intent");
//		super.onNewIntent(intent);
//	}
	
	
	
}
